#include "EngineServer.hpp"

EngineServer::EngineServer(const string & ip, unsigned short port, 
                            size_t threadNum, size_t queSize,
                            size_t cacheCapacity, int cacheSliceNum, int cache_k)
: _server(ip, port)
, _pool(threadNum, queSize)
, _keyWordsRecommender()
, _pageRecommender()
, _keywordCache(cacheCapacity, cacheSliceNum, cache_k)
, _pageCache(cacheCapacity, cacheSliceNum, cache_k)
{

}


EngineServer::~EngineServer()
{
    stop();
}


void EngineServer::setAllCallback(Callback &&cb1, Callback &&cb2, Callback &&cb3)
{
    _server.setAllCallback(std::move(cb1), std::move(cb2), std::move(cb3));
}


void EngineServer::setKeyWordsRecommender(const string &dictFile, const string &indexFile)
{
    _keyWordsRecommender.setKeyWordsRecommender(dictFile, indexFile);
}
    

void EngineServer::setPageRecommender(const string& invertedIndexFile, const string& pageOffsetFile, 
                                      const string& pageLibFile, const string& stopWordFile)
{
    _pageRecommender.setPageRecommender(invertedIndexFile, pageOffsetFile, pageLibFile, stopWordFile);
}


void EngineServer::start()
{
    _pool.start();
    _server.start();
}
    

void EngineServer::stop()
{
    _server.stop();
    _pool.stop();
}


// 根据用户输入内容，分析出相关候选词并返回 (以json格式)
Json EngineServer::getCandidateWords(const string& input)
{
    return _keyWordsRecommender.getCandidateWords(input);
}

// 根据用户输入内容，分析出相关候选网页并返回 (以json指针格式)
shared_ptr<Json> EngineServer::getCandidatePages(const string& input)
{
    return _pageRecommender.getCandidatePages(input);
}


// 向线程池添加任务
void EngineServer::addTask(ElemType && pt)
{
    _pool.addTask(std::move(pt));
}